<template>
  <div class="app-main-content">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input size="small" v-model="dataForm.tableName" placeholder="表名" clearable></el-input>
      </el-form-item>
	    <el-form-item>
        <el-button size="small" type="primary" @click="generator()">生成代码</el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="small" @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :max-height="tableHeight"
      :data="dataList"
      border
	     @selection-change="selectionChangeHandle"
      v-loading="dataListLoading"
      style="width: 100%">
       <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      </el-table-column>
      <el-table-column
        prop="tableName"
        header-align="center"
        align="center"
        label="表名">
      </el-table-column>
      <el-table-column
        prop="engine"
        header-align="center"
        align="center"
        label="Engine">
      </el-table-column>
      <el-table-column
        prop="tableComment"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="表备注">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="创建时间">
      </el-table-column>
    
    </el-table>
    <div class="toolbar">
      <el-pagination background layout="sizes, prev, pager, next, total" 
        @current-change="currentChangeHandle"
        @size-change="sizeChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[20, 30, 50, 100, 200]"
        :page-size="pageSize"
        :total="totalPage" 
        style="float:right;">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import * as $http from '@/api/sys/generator'
  import * as Tool from '@/utils/tool'

  export default {
    data () {
      return {
        tableHeight:Tool.getClientHeight()-200,
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 20,
        totalPage: 0,
        dataListLoading: false,
        selectionDataList: []
      }
    },
    created () {
      const that = this;
      window.addEventListener('resize', function () {
          that.tableHeight = Tool.getClientHeight() - 200;
      });

      this.$nextTick(function(){
          this.getDataList();
      });
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let _this = this,
            postData = Object.assign({},this.dataForm,{
              'page': this.pageIndex,
              'limit': this.pageSize
            })

        $http.getList(postData).then(response => {
          if (response && response.code === 0) {
            _this.dataList = response.page.list
            _this.totalPage = response.page.totalCount
          } else {
            _this.dataList = []
            _this.totalPage = 0
          }
          _this.dataListLoading = false
        }).catch(error => {
            console.log(error);
            _this.dataListLoading = false
        });
      },
	        // 多选
      selectionChangeHandle (val) {
        this.selectionDataList = val
      },
	  generator () {
      
		   let tableNames = this.selectionDataList.map(item => {
               return item.tableName
           })
			if(tableNames == null){
				return ;
			}
			    window.location.href  = process.env.BASE_API+"/sys/generator/code?tables=" + JSON.stringify(tableNames);
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      }
    }
  }
</script>
